using System.Collections.Generic;
using UnityEngine;

//用于在游戏中展示玩家的物品槽信息
public class StaticInventoryDisplay : InventoryDisplay
{
    [SerializeField] private InventoryHolder inventoryHolder;
    [SerializeField] private InventorySlot_UI[] slots;
    int selectedSlot = -1;//快捷栏索引
    private Dictionary<KeyCode, int> slotIndexMap = new Dictionary<KeyCode, int>()
    {
        { KeyCode.Alpha1, 0 },
        { KeyCode.Alpha2, 1 },
        { KeyCode.Alpha3, 2 },
        { KeyCode.Alpha4, 3 },
        { KeyCode.Alpha5, 4 },
        { KeyCode.Alpha6, 5 },
        { KeyCode.Alpha7, 6 },
        { KeyCode.Alpha8, 7 },
        { KeyCode.Alpha9, 8 },
        { KeyCode.Alpha0, 9 }
    };
    
    protected override void Start()
    {
        base.Start();
        RefreshStaticDisplay();

        ChangeSelectedSlot(0);//默认选中第一个槽
    }

    private void Update()
    {
        //快捷栏数字切换
        foreach (var kvp in slotIndexMap)
        {
            if (Input.GetKeyDown(kvp.Key))
            {
                ChangeSelectedSlot(kvp.Value);
                break;
            }
        }

        //使用物品测试
        if (Input.GetKeyDown(KeyCode.Space))
        {
            InventorySlot_UI slot = GetSelectedItem();
            if (slot && slot.AssignedInventorySlot.ItemData)
            {
                Debug.Log("使用了物品:" + slot.AssignedInventorySlot.ItemData.DisplayName);
                // 从物品堆叠中移除一定数量的物品
                slot.AssignedInventorySlot.RemoveFromStack(1);
                slot.UpdateUISlot();// 更新物品槽的显示
            }
            else
            {
                Debug.Log("没有物品可是使用!");
            }
        }
    }

    //切换快捷栏选择
    void ChangeSelectedSlot(int newValue)
    {
        if (selectedSlot >= 0)
        {
            slots[selectedSlot].Deselect();// 取消之前选中的槽位
        }
        slots[newValue].Select();// 选择新的槽位
        selectedSlot = newValue;// 更新选中的槽位索引
    }

    // 获取当前选中快捷栏信息
    public InventorySlot_UI GetSelectedItem()
    {
        if (slots.Length > 0)
        {
            InventorySlot_UI slot = slots[selectedSlot];// 获取当前选中槽位
            if (slot != null) return slot;// 如果有物品，则返回物品
        }
        return null;//如果没有选中物品则返回null
    }

    private void OnEnable()
    {
        PlayerInventoryHolder.OnPLayerInventoryChanged += RefreshStaticDisplay;
    }

    private void OnDisable()
    {
        PlayerInventoryHolder.OnPLayerInventoryChanged -= RefreshStaticDisplay;
    }
    private void RefreshStaticDisplay()
    {
        if (inventoryHolder != null)
        {
            inventorySystem = inventoryHolder.PrimaryInventorySystem;
            inventorySystem.OnInventorySlotChanged += UpdateSlot;
        }
        else Debug.LogWarning($"没有存货分配给 {gameObject}");
        AssignSlot(inventorySystem, 0);
    }

    // 实现基类方法，为每个物品槽赋值 InventorySlot,用于将 InventorySystem 中的物品槽数据分配给对应的物品槽组件（InventorySlot_UI）
    public override void AssignSlot(InventorySystem invToDisplay, int offset)
    {
        slotDictionary = new Dictionary<InventorySlot_UI, InventorySlot>();

        // 遍历物品槽数组，为每个 InventorySlot_UI 赋值 InventorySlot，并初始化物品槽
        for (int i = 0; i < inventoryHolder.Offset; i++)
        {
            slotDictionary.Add(slots[i], inventorySystem.InventorySlots[i]);
            slots[i].Init(inventorySystem.InventorySlots[i]);
        }
    }
}
